package com.example.android2019.homework.homework3;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.android2019.R;
import com.example.android2019.ch3.MyHelper;

public class ConstraintLayout extends AppCompatActivity {
    Button button_tj, button_cx, button_xg, button_sc;
    MyHelper myHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_constraint);
        /*关联控件*/
        inti();

        myHelper=new MyHelper(this);
        db=myHelper.getWritableDatabase();

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.button_tj:
                        ContentValues cv = new ContentValues();
                        cv.put("name", "list1");
                        cv.put("phone", "1234456789");
                        db.insert("user", null, cv);
                        Toast.makeText(ConstraintLayout.this, "添加成功", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.button_sc:
                        db.delete("user", "name=?", new String[]{"list1"});
                        Toast.makeText(ConstraintLayout.this, "删除成功", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.button_xg:
                        ContentValues cv1 = new ContentValues();
                        cv1.put("name", "lid");
                        cv1.put("phone", "1235557890");
                        db.update("user", cv1, "name=?", new String[]{"list1"});
                        Toast.makeText(ConstraintLayout.this, "更新成功", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.button_cx:
                        Cursor cursor = db.query("user", new String[]{"name", "phone"}, null, null, null, null, null, null);
                        while (cursor.moveToNext()) {
                            String name = cursor.getString(cursor.getColumnIndex("name"));
                            String sex = cursor.getString(cursor.getColumnIndex("sex"));
                            Toast.makeText(ConstraintLayout.this, name + "::" + sex, Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }
        };
        button_tj.setOnClickListener(listener);
        button_sc.setOnClickListener(listener);
        button_xg.setOnClickListener(listener);
        button_cx.setOnClickListener(listener);
    }

    private void inti() {
        button_tj = findViewById(R.id.button_tj);
        button_sc = findViewById(R.id.button_sc);
        button_xg = findViewById(R.id.button_xg);
        button_cx = findViewById(R.id.button_cx);
    }
}
